package com.swm.julie.rest.sausage.resource;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Formatter;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.xmlrpc.XmlRpcException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.swm.julie.rest.sausage.model.Product;
import com.swm.julie.rest.sausage.service.XMLRPCSearchProdutImpl;

@Path("/search")

@Component
public class SausageResourceSearchImpl implements SausageResourceSearch{

	Logger logger = LoggerFactory.getLogger(SausageResourceSearchImpl.class);
	
	//standard search
	@GET
	@Path("standard")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public ArrayList<Product> standardSearch(@QueryParam("q") String query)
	{
		ArrayList<Product> res = new ArrayList<Product>();
		
		try 
		{
			res = XMLRPCSearchProdutImpl.standardSearch(query);
		} catch (MalformedURLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlRpcException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		logger.info("standardSearch : %s, hits %d", query, res.size());
		logger.info(new Formatter().format("standardSearch : %s, hits %d", query, res.size()).toString());

		return res;
	}
	
	@Override
	@GET
	@Path("item/item")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public ArrayList<Product> itemSearch(@HeaderParam("item") String item, @HeaderParam("query") String query)
			throws MalformedURLException, XmlRpcException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		ArrayList<Product> res = XMLRPCSearchProdutImpl.itemSearch(item, URLDecoder.decode(query, "UTF-8"));
		logger.info(new Formatter().format("itemSearch : %s, hits %d", query, res.size()).toString());
		
		return res;
	}
	
	@GET
	@Path("company")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	@Override
	public ArrayList<Product> companySearch(@HeaderParam("company") String query)
//	public Response companySearch(@HeaderParam("company") String query)
			throws MalformedURLException, XmlRpcException {
		// TODO Auto-generated method stub
		
		ArrayList<Product> res = XMLRPCSearchProdutImpl.companySearch(query);
//		logger.info("company search : %s, hits %d", query, res.size());
		logger.info(new Formatter().format("company search : %s, hits %d", query, res.size()).toString());
		
		return res;
	}
	
	@GET
	@Path("company/item")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	@Override
	public ArrayList<Product> itemCompanySearch(@HeaderParam("company") String company, @HeaderParam("query") String query)
			throws MalformedURLException, XmlRpcException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		
		ArrayList<Product> res = XMLRPCSearchProdutImpl.itemCompanySearch(company, URLDecoder.decode(query, "UTF-8"));
//		logger.info("itemCompanySearch : %s, hits %d", query, res.size());
		logger.info(new Formatter().format("itemCompanySearch : %s, hits %d", query, res.size()).toString());
		
		return res;
	}
	
	@GET
	@Path("province")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	@Override
	public ArrayList<Product> provinceSearch(@HeaderParam("item") String query)
			throws MalformedURLException, XmlRpcException {
		// TODO Auto-generated method stub
		ArrayList<Product> res = XMLRPCSearchProdutImpl.provinceSearch(query);

		logger.info(new Formatter().format("provinceSearch : %s, hits %d", query, res.size()).toString());
		return res;
	}
	
	@GET
	@Path("province/item")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	@Override
	public ArrayList<Product> itemProvinceSearch(@HeaderParam("province") String province, @HeaderParam("query") String query)
			throws MalformedURLException, XmlRpcException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		ArrayList<Product> res = XMLRPCSearchProdutImpl.itemPovinceSearch(province, URLDecoder.decode(query, "UTF-8"));
		
		logger.info(new Formatter().format("itemProvinceSearch : %s province, %s query, hits %d",  province, query, res.size()).toString());
		return res;
	}
}
